package com.banciyuan.bcywebview.biz.main.mineinfo.drafts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.dialog.ConfirmDialog;
import com.banciyuan.bcywebview.biz.post.write.WriteArticleActivity;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.google.gson.Gson;

import java.util.List;

import de.greenrobot.daoexample.model.DraftsPostItem;
import de.greenrobot.daoexample.model.PostItem;

/**
 * Created by hy on 2016/4/5.
 */
public class DraftsAdapter extends BaseAdapter {
    private Context context;
    private List<DraftsPostItem> novelDrafts;
    private LayoutInflater mLayoutInflater;
    private ImageLoader imageLoader;
    private String uid;
    private Gson gson;

    public DraftsAdapter(Context context, List<DraftsPostItem> novelDrafts) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.novelDrafts = novelDrafts;
        imageLoader = ImageLoader.getInstance();
        uid = UserDataHelper.getInstance(context).getUid();
        gson = new Gson();
    }

    @Override
    public int getCount() {
        return novelDrafts.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        DraftsPostItem draftsPostItem = novelDrafts.get(position);
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.drafts_item, parent, false);
            mViewHolder.ivCover = (ImageView) convertView.findViewById(R.id.iv_novel_cover);
            mViewHolder.tvNovelTitle = (TextView) convertView.findViewById(R.id.tv_novel_title);
            mViewHolder.tvNovelTime = (TextView) convertView.findViewById(R.id.tv_novel_update_time);
            mViewHolder.viewDeleteNovel = convertView.findViewById(R.id.iv_delete_novel_drafts);
            mViewHolder.viewEditeNovel = convertView.findViewById(R.id.iv_edit_novel_drafts);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        PostItem postItem = draftsPostItem.getPostItem();
        if (TextUtils.isEmpty(postItem.getPath())) {
            mViewHolder.ivCover.setImageResource(R.drawable.drafts_default_cover);
        } else {
            imageLoader.displayImage(postItem.getPath(), mViewHolder.ivCover, ImageOptions.options);
        }
        if (TextUtils.isEmpty(postItem.getTitle())) {
            StringBuffer sb = new StringBuffer();
            for (String s : postItem.getContent()) {
                sb.append(Html.fromHtml(s));
            }
            mViewHolder.tvNovelTitle.setText(sb.toString());
        } else {
            mViewHolder.tvNovelTitle.setText(postItem.getTitle());
        }
        mViewHolder.tvNovelTime.setText(context.getString(R.string.save_at) + StringUtil.getOppositeTime(String.valueOf(draftsPostItem.getUpdateTime())));
        OnClick onclick = new OnClick(position);
        mViewHolder.viewDeleteNovel.setOnClickListener(onclick);
        mViewHolder.viewEditeNovel.setOnClickListener(onclick);
        return convertView;
    }

    private static class ViewHolder {
        ImageView ivCover;
        TextView tvNovelTitle;
        TextView tvNovelTime;
        View viewDeleteNovel;
        View viewEditeNovel;
    }

    class OnClick implements View.OnClickListener {
        int position;

        public OnClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_delete_novel_drafts:
                    showDeleteDialog();
                    break;
                case R.id.iv_edit_novel_drafts:
                    Intent intent = new Intent(context, WriteArticleActivity.class);
                    intent.putExtra("drafts_postitem", novelDrafts.get(position));
                    ((Activity) context).startActivityForResult(intent, DraftsActivity.GO_EDIT_DRAFTS);
                    break;
            }
        }

        private void showDeleteDialog() {
            View.OnClickListener comfirmAction = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    novelDrafts.remove(position);
                    SPHelper.putString(context, SPConstant.SPNAME_NOVELDRAFTS + uid, SPConstant.NOVEL, gson.toJson(novelDrafts));
                    SPHelper.putString(context, SPConstant.SPNAME_NOVELDRAFTS + uid, SPConstant.NOVEL_COUNT, String.valueOf(novelDrafts.size()));
                    notifyDataSetChanged();
                }
            };
            ConfirmDialog.Builder builder = new ConfirmDialog.Builder(context);
            builder.setDescString(context.getString(R.string.confirm_delete_drafts))
                    .setActionString(context.getString(R.string.mydialog_delete))
                    .setCancelString(context.getString(R.string.mydialog_cancel))
                    .setActionClickListener(comfirmAction).create().show();
        }
    }
}
